package com.xinli.xinlifm.view.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.xinli.xinlifm.R;
import com.xinli.xinlifm.http.bean.DianTaiBean;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by XiaoLiu on 2016/10/15.
 */

public class DiantaiAdapter extends SuperRecyclerAdapter<Object,DiantaiAdapter.ViewHolder> {
    public DiantaiAdapter(Context context, List<Object> list) {
        super(context, list, new int[]{R.layout.discover_viewpager_item,R.layout.discover_anchor_item,R.layout.discover_hot_anchor});
    }

    @Override
    public int getToChild(int position) {
        if(position == 0){
            return layoutId[0];
        }else if(position == 1){
             return layoutId[1];
        }else if(position==2){
            return layoutId[2];
        }
        return 0;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(viewType,parent,false);
        Log.e("自定义标签", "类名==DiantaiAdapter" + "方法名==onCreateViewHolder=====:" + viewType);
        Log.e("自定义标签", "类名==DiantaiAdapter" + "方法名==onCreateViewHolder=====:" + (viewType==layoutId[1]));
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, int position) {
        if(position == list.size()){
            return;
        }
        Object o = list.get(position);
        ArrayList o1 = (ArrayList) o;
        if (o1.get(0) instanceof DianTaiBean.DataBean.TuijianBean) {
            if (holder.viewPager.getAdapter() == null) {
                ArrayList<DianTaiBean.DataBean.TuijianBean> tuijianBeen = (ArrayList<DianTaiBean.DataBean.TuijianBean>) o;
                tuijianBeen.addAll(tuijianBeen);
                DianTaiPagerAdapter dianTaiPagerAdapter = new DianTaiPagerAdapter(tuijianBeen);
                holder.viewPager.setAdapter(dianTaiPagerAdapter);
                for (DianTaiBean.DataBean.TuijianBean bean : tuijianBeen) {
                    ImageView imageView = new ImageView(context);
                    imageView.setImageResource(R.drawable.point);
                    int v = (int) context.getResources().getDimension(R.dimen.point_padding);
                    imageView.setPadding(v, v, v, v);
                    holder.discover_viewpager_lin.addView(imageView);
                }
                holder.viewPager.addOnPageChangeListener(holder);
                holder.viewPager.setCurrentItem(1);
                holder.discover_viewpager_img.post(new Runnable() {
                    @Override
                    public void run() {
                        ViewCompat.setTranslationX(holder.discover_viewpager_img, holder.discover_viewpager_lin.getLeft());
                    }
                });
                holder.handler.obtainMessage(0, 1, 0).sendToTarget();
            }
        }else if(o1.get(0) instanceof DianTaiBean.DataBean.NewlistBean){
            Log.e("自定义标签", "类名==DiantaiAdapter" + "方法名==onBindViewHolder=====:" + o1);
            holder.anchor_container.removeAllViews();
            int length = o1.size();
            for (int size = 0; size < length; size++) {
                View view= LayoutInflater.from(context).inflate(R.layout.discover_anchor_one_item,holder.anchor_container,false);
                DianTaiBean.DataBean.NewlistBean newlistBean = (DianTaiBean.DataBean.NewlistBean) o1.get(size);
                SimpleDraweeView dra = (SimpleDraweeView) view.findViewById(R.id.discover_anchor_img1);
                dra.setImageURI(newlistBean.getCover());
                ((TextView) view.findViewById(R.id.discover_anchor_author1)).setText(newlistBean.getTitle()+"");
                holder.anchor_container.addView(view);
            }
        }else if(o1.get(0) instanceof DianTaiBean.DataBean.HotlistBean){
            holder.discover_hot_anchor_container.removeAllViews();
            int length = o1.size();
            for (int i = 0; i < length; i++) {
                View view = LayoutInflater.from(context).inflate(R.layout.discover_hot_anchor_item,null);
                DianTaiBean.DataBean.HotlistBean bean = ((DianTaiBean.DataBean.HotlistBean) o1.get(i));
                TextView hot_anchor_author = (TextView) view .findViewById(R.id.hot_anchor_author);
                hot_anchor_author.setText(bean.getTitle());
                ((SimpleDraweeView) view.findViewById(R.id.hot_anchor_img1)).setImageURI(bean.getCover());
                TextView hot_anchor_intr = (TextView) view.findViewById(R.id.hot_anchor_intr);
                hot_anchor_intr.setText(bean.getContent());
                TextView hot_anchor_pro = (TextView) view.findViewById(R.id.hot_anchor_pro);
                hot_anchor_pro.setText("节目"+bean.getFmnum());
                holder.discover_hot_anchor_container.addView(view);
            }
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder implements ViewPager.OnPageChangeListener,Handler.Callback{

        private Handler handler = new Handler(this);

        private ViewPager viewPager;
        private LinearLayout discover_viewpager_lin;
        private ImageView discover_viewpager_img;

        private LinearLayout anchor_container;

        private LinearLayout discover_hot_anchor_container;


        public ViewHolder(View itemView) {
            super(itemView);

            viewPager = (ViewPager) itemView.findViewById(R.id.discover_viewpager);
            discover_viewpager_lin = ((LinearLayout) itemView.findViewById(R.id.discover_viewpager_lin));
            discover_viewpager_img = ((ImageView) itemView.findViewById(R.id.discoverviewpager_img));

            anchor_container = (LinearLayout) itemView.findViewById(R.id.anchor_container);

            discover_hot_anchor_container = (LinearLayout) itemView.findViewById(R.id.discover_hot_anchor_container);

        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            float value = discover_viewpager_lin.getLeft() + (position - 1 + positionOffset) * discover_viewpager_img.getWidth();
            value = Math.max(value, discover_viewpager_lin.getLeft());
            value = Math.min(value, discover_viewpager_lin.getRight() - discover_viewpager_img.getWidth());
            ViewCompat.setTranslationX(discover_viewpager_img, value);
        }

        @Override
        public void onPageSelected(int position) {
            PagerAdapter adapter = viewPager.getAdapter();
            if (position == 0) {
                viewPager.setCurrentItem(adapter.getCount() - 2, false);
            }
            else if (position == adapter.getCount() - 1) {
                viewPager.setCurrentItem(1, false);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }

        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    int arg1 = msg.arg1;
                    if (arg1 >viewPager.getAdapter().getCount() - 2) {
                        arg1 = 1;
                    }
                    viewPager.setCurrentItem(arg1, arg1 != 1);
                    Message message = handler.obtainMessage(0, ++arg1, 0);
                    handler.sendMessageDelayed(message, 2000);
                    break;
            }
            return true;
        }
    }
}
